package design.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class TestAop implements InvocationHandler {
    private final Object target;

    public TestAop(Object target) {
        this.target = target;
    }

    public Object getProxy() {
        // 取得代理对象
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("AOP增强方法");
        return method.invoke(this.target, args);
    }

    public static void main(String[] args) {
        TestAop testAop = new TestAop(new TestServiceImpl());
        TestService proxy = (TestService) testAop.getProxy();
        System.out.println(proxy.test());
    }

}
